package com.bawei.module_mine.adapter

import android.app.AlertDialog
import android.graphics.Bitmap
import android.media.ThumbnailUtils
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import java.io.File
import com.bawei.module_mine.R

class OfflineCacheAdapter(private var fileList: List<File>) : RecyclerView.Adapter<OfflineCacheAdapter.ViewHolder>() {
    // item点击回调
    private var onItemClickListener: ((File) -> Unit)? = null
    // item长按回调
    private var onItemLongClickListener: ((File) -> Unit)? = null

    /** 设置点击监听 */
    fun setOnItemClickListener(listener: (File) -> Unit) {
        onItemClickListener = listener
    }
    /** 设置长按监听 */
    fun setOnItemLongClickListener(listener: (File) -> Unit) {
        onItemLongClickListener = listener
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        // 视频缩略图
        val ivThumb: ImageView = itemView.findViewById(R.id.iv_offline_thumb)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_offline_cache, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val file = fileList[position]
        // 生成视频缩略图
        val thumb: Bitmap? = ThumbnailUtils.createVideoThumbnail(file.absolutePath, MediaStore.Images.Thumbnails.MINI_KIND)
        holder.ivThumb.setImageBitmap(thumb)
        // 点击事件
        holder.itemView.setOnClickListener {
            onItemClickListener?.invoke(file)
        }
        // 长按事件
        holder.itemView.setOnLongClickListener {
            onItemLongClickListener?.invoke(file)
            true
        }
    }

    override fun getItemCount(): Int = fileList.size

    /** 刷新数据 */
    fun updateData(newList: List<File>) {
        fileList = newList
        notifyDataSetChanged()
    }
} 